package oop.core.view;

import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.imageio.ImageIO;

/**
 * Handles images of all the items and objects on the map.
 *
 */
public class ImageHandler {

	public static Map<String, Image> imgTable = new HashMap<String, Image>();
	
	public static void main(String[] args)
	{
		File fileDir = new File("img/");
		loadImages(fileDir);
		Set<String> keySet = imgTable.keySet();
		Iterator<String> it = keySet.iterator();
		while(it.hasNext())
		{
			String s = it.next();
			System.out.println(s + ":                                     " + imgTable.get(s));
		}
	}
	
	public static void loadImages()
	{
		File fileDir = new File("img/");
		loadImages(fileDir);
	}
	
	private static void loadImages(File fileDir)
	{
		
		File[] files = fileDir.listFiles();
		
		for(int k=0; k<files.length; k++)
		{
			String fileName = files[k].getName();
			if(fileName.equals(".svn"))
				continue;
			
			if(files[k].isDirectory())
			{
				loadImages(files[k]);
			}
			else
			{
				Image image = null;
				try
				{
						image = ImageIO.read(files[k]);
				}
				catch (IOException ioe)
				{
					ioe.printStackTrace();
					continue;
				}
				
				String name = files[k].getName();
				
				if(name.indexOf(".png") != -1)
				{
					name = name.substring(0, name.indexOf(".png"));
					imgTable.put(name, image);
				}
			}
		}
	}
	
	public static Image getImage(String imageID)
	{
		return imgTable.get(imageID);
	}
	
}
